package com.whmskj.xjlhbc.mobile.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whmskj.xjlhbc.core.AjaxResult;
import com.whmskj.xjlhbc.domain.Bctj18wshopydqk;
import com.whmskj.xjlhbc.domain.Bctj18wshopydday;
import com.whmskj.xjlhbc.mapper.Bctj18wshopydqkMapper;
import com.whmskj.xjlhbc.mapper.Bctj18wshopyddayMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@Tag(name = "用电情况", description = "BC-TJ-18 车间用电情况查询")
@RequestMapping("/mobile/electric")
public class ElectricityReportController {

    @Autowired
    private Bctj18wshopydqkMapper monthMapper;

    @Autowired
    private Bctj18wshopyddayMapper dayMapper;

    @GetMapping("/month")
    @Operation(summary = "月用电情况查询")
    public AjaxResult queryMonth(@RequestParam Integer year,
                                 @RequestParam Integer month) {
        LambdaQueryWrapper<Bctj18wshopydqk> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Bctj18wshopydqk::getYear, year)
                .eq(Bctj18wshopydqk::getMonth, month);
        List<Bctj18wshopydqk> list = monthMapper.selectList(wrapper);
        return AjaxResult.success(list);
    }

    @GetMapping("/day/current")
    @Operation(summary = "当前盘存月的日清单（可选传入区间）")
    public AjaxResult queryCurrentInventoryDayList(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate){
        DateTime start;
        DateTime end;
        if(startDate != null && endDate != null){
            start = DateUtil.beginOfDay(DateUtil.date(startDate));
            end = DateUtil.beginOfDay(DateUtil.date(endDate));
        }else{
            DateTime now = DateUtil.date();
            int day = now.dayOfMonth();
            DateTime label = day < 21 ? now : DateUtil.nextMonth();
            start = DateUtil.offsetMonth(label.setField(DateField.DAY_OF_MONTH, 21), -1);
            end = label.setField(DateField.DAY_OF_MONTH, 20);
        }

        LambdaQueryWrapper<Bctj18wshopydday> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(Bctj18wshopydday::getStatisticsDate, start.toDateStr())
                .le(Bctj18wshopydday::getStatisticsDate, end.toDateStr())
                .orderByAsc(Bctj18wshopydday::getStatisticsDate);
        List<Bctj18wshopydday> list = dayMapper.selectList(wrapper);

        Map<String,Object> resp = new LinkedHashMap<>();
        resp.put("startDate", start.toDateStr());
        resp.put("endDate", end.toDateStr());
        resp.put("list", list);
        // 提示：总用电页签的“万米用电”请取字段 msjwmyd（实际万米用电）
        return AjaxResult.success(resp);
    }
}


